package com.sangmo.fts.api.inventory;

import com.sangmo.boot.framework.api.utils.ApiResponse;
import com.sangmo.boot.framework.common.security.meta.Access;
import com.sangmo.boot.framework.data.filter.PageResult;
import com.sangmo.boot.framework.data.filter.ValueFilter;
import com.sangmo.boot.framework.plugin.dataview.data.DataViewPageSearch;
import com.sangmo.fts.process.inventory.interfaces.IfInboundStoreRequest;
import com.sangmo.fts.process.inventory.model.IfInbound;
import com.sangmo.fts.process.inventory.model.IfLadingBillItem;
import com.sangmo.fts.process.inventory.service.IfInboundService;
import com.sangmo.fts.restructure.meta.TrainingApi;
import com.sangmo.fts.restructure.meta.TrainingProcess;
import com.sangmo.fts.training.model.TrainingHistory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.validation.Valid;
import java.util.HashMap;

@TrainingApi
@Controller
@RequestMapping(value = "/ifInbound/v1")
public class IfInboundApi {

    @Autowired
    private IfInboundService ifInboundService;

    @Access(":read")
    @PostMapping("/{trainingId}/search")
    public @ResponseBody ApiResponse<PageResult<IfInbound>> search(@PathVariable("trainingId") Long trainingId, @RequestBody @Valid DataViewPageSearch<HashMap> ps) {
        ps.getFilters().add(new ValueFilter("trainingId", ValueFilter.OP_EQ, trainingId));
        return ApiResponse.make(ifInboundService.search(ps));
    }

    @Access(":read")
    @PostMapping("/{trainingId}/{id}/goodsItems/list")
    public @ResponseBody ApiResponse<PageResult<IfLadingBillItem>> listGoodsItems(@PathVariable("trainingId") Long trainingId, @PathVariable("id") Long id, @RequestBody @Valid DataViewPageSearch<HashMap> ps) {
        return ApiResponse.make(ifInboundService.searchGoods(id, ps));
    }

    @Access(":receive")
    @PostMapping(value = "/{trainingId}/{id}/receive")
    public @ResponseBody
    ApiResponse receive(@PathVariable("trainingId") Long trainingId, @PathVariable("id") Long id) {
        ifInboundService.receive(id);
        return ApiResponse.make();
    }

    @TrainingProcess(node = TrainingHistory.IfProcessNodes.IF_INBOUND_SELLER_STORE_16)
    @Access(":store")
    @PostMapping(value = "/{trainingId}/{id}/store")
    public @ResponseBody
    ApiResponse store(@PathVariable("trainingId") Long trainingId, @PathVariable("id") Long id, @RequestBody @Valid IfInboundStoreRequest request) {
        ifInboundService.store(id, request);
        return ApiResponse.make();
    }

}
